import Vue from 'vue'
import Vuex from 'vuex'
import { get_song_detail } from '../API/common.js';
import {get_lyric} from '../API/common.js';

Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		play_state: false,
		//表示当前正在播放的音频相关信息
		song_info: {
			picUrl:'https://p2.music.126.net/xFuNzlIR5D3vMhigqx4EOw==/109951164412414795.jpg'
		},
		//存放当前正在播放列表的歌曲id集合
		play_list:[
			
		],
		song_idx: 0,
		song_duration_info:{
			current_time:0,
			duration: 1,
		},
		active_lyric_index:0,
		lyric_info:{
			lyric_schedule:[],
			lyric_divided:[],
			
		},
		timeupdate_switch:true,
	},
	mutations: {
		change_state(state, is_playing) {
			state.play_state = is_playing;
		},
		set_song_info(state, info) {
			state.song_info = info;
		},
		set_play_list(state, list) {
			state.play_list = list;
		},
		change_idx(state, val) {
			state.song_idx = val;
		},
		set_song_duration_info(state, song_duration_info){
			state.song_duration_info = song_duration_info;
		},
		set_lyric_info(state, lyric_info){
			state.lyric_info = lyric_info;
		},
		lyric_play(state, current_time){
			// 判断currtime 在哪个区间, 与 时刻表所有 元素做差值
			let D_value = [];
			state.lyric_info.lyric_schedule.map(el => {
			D_value.push(Math.abs(el - current_time));	
			})
			// console.log(D_value);
			let index = D_value.indexOf(Math.min(...D_value));
			// console.log(current_time, state.lyric_info.lyric_schedule[index]);
			if(current_time - state.lyric_info.lyric_schedule[index] < 0)
			{
				state.active_lyric_index = index - 1;
				// console.log(index, '小于0');
			} else {
				state.active_lyric_index = index;
				// console.log(index,'大于等于0');
			}
		},
		lyric_to_idx(state, idx){
			state.active_lyric_index = idx;
		},
		change_update_switch(state, bool){
			state.timeupdate_switch = bool;
		}
	},
	actions: {
		async	get_song_detail(context, id){
			// let check_result = await check_music();
			
			
			// 需要将当前音频的相关信息存执 store , 歌名 ar id 等
			// 点击播放时, 要将播放列表切换至当前歌曲所在歌单, 方便后面做上一首 下一首/
				let song_detail = await get_song_detail(id);
				// console.log(res,"NNNNNNNNNNNNNNNNN")
				let current_song = song_detail.data.songs[0];

				context.commit("set_song_info", {
					album_name: current_song.al.name,
					id: current_song.id,
					song_name: current_song.name,
					ars: current_song.ar,
					picUrl: current_song.al.picUrl,
					
				});
		},
		async get_lyric(context, id){
				let lyric_data = await get_lyric(id);
				let lyric = lyric_data.data.lrc.lyric;
				console.log(lyric[12],'|||||||||||||||||',lyric);
				let lyric_schedule = [];
				let lyric_divided = [];
				let left_brackets_idx = -1;
				let right_brackets_idx = -1;
				let next_left_bracket_idx = 0;
				let temp_time = ""; 
				// let time = 0;
				// console.log(lyric.indexOf('['));
				// console.log(lyric.indexOf(']'));
				// console.log(lyric.substring(lyric.indexOf('[')+1,lyric.indexOf(']')));
				for(;;){
					//获取 歌词的 时刻表 
					// console.log(time++);
					left_brackets_idx = lyric.indexOf('[', left_brackets_idx + 1);
					if(left_brackets_idx == -1){
						break;
					}
					right_brackets_idx = lyric.indexOf(']', right_brackets_idx + 1);
					//找到下一个左括号的下标
					next_left_bracket_idx = lyric.indexOf('[', left_brackets_idx + 1);
					if(next_left_bracket_idx == -1){
						lyric_divided.push(lyric.substring(right_brackets_idx + 1))	;
					} else {
						lyric_divided.push(lyric.substring(right_brackets_idx + 1, next_left_bracket_idx));
					}
					//截取歌词

					temp_time = (lyric.substring(left_brackets_idx+1, right_brackets_idx));
					lyric_schedule.push(temp_time);

					
				}
				// console.log(lyric_schedule, lyric_schedule[2]);
			// 	console.log(lyric_divided);
				
				// console.log(lyric_schedule[2].split(".")[1],'分s',Number(lyric_schedule[0].split(":")[1]),'秒');	
				//处理 时刻表的分秒， 将其转变为 时间戳
				lyric_schedule = lyric_schedule.map(el => {
					
					let min = Number(el.split(":")[0]);
					let sec = Number((el.split(":")[1]+"").substring(0,2));
					let haomiao =  Number(el.split(".")[1]);
					return min * 60 * 1000 + sec * 1000 + haomiao;
				})
				// console.log(lyric_schedule);
				// console.log(lyric_divided)
				context.commit('set_lyric_info', {
					lyric_schedule,
					lyric_divided,
				});
				
		}
	},
	modules: {}
})